home *** CD-ROM | disk | FTP | other *** search
- /*
- * ScoreBoard.c - "Boxes" scoreboard manager
- *
- * Port is assumed to be set correctly for routines that draw anything.
- */
-
-
- # include "Boxes.h"
-
-
- # define frameThickness 2
- # define tHeight 20 /* title height */
- # define pHeight 20 /* title height */
- # define pWidth 50 /* player width */
- # define sWidth 50 /* score width */
- # define sbWidth (pWidth + sWidth + 3 * frameThickness)
-
- static short nPlayers = 2;
- static short player;
- static short score[maxPlayer];
- static Rect sbRect; /* scoreboard rectangle */
- static Rect tRect; /* title rectangle */
- static Rect pRect; /* player rectangle */
- static Rect sRect; /* score rectangle */
-
- static RgnHandle arrowRgn = (RgnHandle) nil;
-
-
- /* Local routine prototypes */
-
- static void DrawPlayer (short i);
- static void HilitePlayer (short i);
- static void HiliteScore (short i);
-
-
- short
- GetPlayerCount (void)
- {
- return (nPlayers);
- }
-
-
- void
- SetPlayerCount (short n)
- {
- nPlayers = n;
- if (nPlayers < 1)
- nPlayers = 1;
- if (nPlayers > maxPlayer)
- nPlayers = maxPlayer;
- }
-
-
- void
- InitializeScoreBoard (void)
- {
- short i;
-
- player = 0;
- for (i = 0; i < nPlayers; i++)
- score[i] = 0;
- if (arrowRgn == nil)
- {
- /*
- Create arrow region
- */
- arrowRgn = NewRgn ();
- OpenRgn ();
- MoveTo (0, 3);
- LineTo (4, 3);
- LineTo (4, 0);
- LineTo (12, 7);
- LineTo (4, 14);
- LineTo (4, 11);
- LineTo (0, 11);
- LineTo (0, 3);
- CloseRgn (arrowRgn);
- }
- }
-
-
- /*
- * Calculate size of scoreboard. (With upper left at (0, 0), i.e., this
- * is not in the position it needs to be
- */
-
- void
- CalcScoreBoardSize (Rect *r)
- {
- short depth;
-
- depth = tHeight + pHeight * nPlayers + frameThickness * 3;
- SetRect (r, 0, 0, sbWidth, depth);
- }
-
-
- /*
- * Figure out where scoreboard should be located and calculate the positions
- * of some of its pieces.
- */
-
- void
- PlaceScoreBoard (short h, short v)
- {
- CalcScoreBoardSize (&sbRect);
- OffsetRect (&sbRect, h, v);
- SetRect (&tRect, 0, 0, sbWidth, tHeight);
- OffsetRect (&tRect, sbRect.left, sbRect.top);
- InsetRect (&tRect, frameThickness, frameThickness);
- SetRect (&pRect, 0, 0, sbWidth, tHeight);
- OffsetRect (&pRect, sbRect.left, sbRect.top + 2 * frameThickness + tHeight);
- InsetRect (&pRect, frameThickness, 0);
- sRect = pRect;
- pRect.right = pRect.left + pWidth;
- sRect.left = sRect.right - sWidth;
- }
-
-
- void
- NextPlayer (void)
- {
- DrawPlayer (player);
- player = ++player % nPlayers;
- HilitePlayer (player);
- }
-
-
- short
- GetPlayer (void)
- {
- return (player);
- }
-
-
- void
- IncrementScore (short player, short n)
- {
- score[player] += n;
- }
-
-
- static void
- DrawPlayer (short i)
- {
- Rect r;
- Str255 s;
-
- NumToString ((long) i + 1, s);
- r = pRect;
- OffsetRect (&r, 0, i * pHeight);
- EraseRect (&r);
- MoveTo (r.right - StringWidth (s) - 5, r.bottom - fontInfo.descent);
- SetColor (GetPlayerColor (i));
- DrawString (s);
- RestoreColor ();
- }
-
-
- static void
- HilitePlayer (short i)
- {
- DrawPlayer (i);
- OffsetRgn (arrowRgn, pRect.left + 5, pRect.top + i * pHeight + 5);
- SetColor (GetPlayerColor (i));
- PaintRgn (arrowRgn);
- RestoreColor ();
- OffsetRgn (arrowRgn, -(pRect.left + 5), -(pRect.top + i * pHeight + 5));
- }
-
-
- void
- DrawScore (short i)
- {
- Rect r;
- Str255 s;
-
- NumToString ((long) score[i], s);
- r = sRect;
- OffsetRect (&r, 0, i * pHeight);
- EraseRect (&r);
- MoveTo (r.right - StringWidth (s) - 5, r.bottom - fontInfo.descent);
- SetColor (GetPlayerColor (i));
- DrawString (s);
- RestoreColor ();
- }
-
-
- static void
- HiliteScore (short i)
- {
- DrawScore (i);
- OffsetRgn (arrowRgn, sRect.left + 5, sRect.top + i * pHeight + 5);
- SetColor (GetPlayerColor (i));
- PaintRgn (arrowRgn);
- RestoreColor ();
- OffsetRgn (arrowRgn, -(sRect.left + 5), -(sRect.top + i * pHeight + 5));
- }
-
-
- void
- DrawScoreBoard (void)
- {
- short left;
- short i, max;
- Rect r;
-
- EraseRect (&sbRect);
- /* draw frame */
- SetColor (GetScoreboardColor ());
- PenSize (frameThickness, frameThickness);
- r = sbRect;
- FrameRect (&r);
- r.bottom = r.top + tHeight + 2 * frameThickness;
- FrameRect (&r);
- r = sbRect;
- r.right = r.left + pWidth + 2 * frameThickness;
- FrameRect (&r);
- PenNormal ();
- /* draw title */
- r = tRect;
- r.right = r.left + pWidth;
- left = r.left + (r.right - r.left - StringWidth ("\pPlayer")) / 2;
- MoveTo (left, r.bottom - fontInfo.descent);
- DrawString ("\pPlayer");
- r = tRect;
- r.left = r.right - sWidth;
- left = r.left + (r.right - r.left - StringWidth ("\pScore")) / 2;
- MoveTo (left, r.bottom - fontInfo.descent);
- DrawString ("\pScore");
- RestoreColor ();
-
- for (i = 0; i < nPlayers; i++)
- {
- DrawPlayer (i);
- DrawScore (i);
- }
- if (SidesLeft () > 0) /* game not over */
- HilitePlayer (player);
- else
- {
- for (max = 0, i = 0; i < nPlayers; i++)
- {
- if (max < score[i])
- max = score[i];
- }
- for (i = 0; i < nPlayers; i++)
- {
- if (max == score[i])
- HiliteScore (i);
- }
- }
- }
-